<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">-->
<title>角色权限</title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<jsp:include page="../common/import.jsp"></jsp:include>

</head>
<body>
          <div class="content pt0">
              <div class="right-cot w100" style="padding-left:20px;">
                  
                  <input type="hidden" value="${roleId }" id="_roleId" />
                  <div class="form-top">
                        <div class="mb10">
                           应用： <select id="_appId" name="appId" class="js-example-basic-single form-control-select" onchange="loadMenus()" >
                                      
										<c:forEach items="${appList}" var="a">
											<option value="${a.id }"  >${a.appName }</option>
										</c:forEach>
                                  </select>
                                   <c:if test="${empty appList }">
                                  <span style="color:red">请先给角色赋应用</span>
                                  </c:if>
                        </div>
                  </div>
                  <div style="width:100%;"  id="menuDetail">
                 
                  </div>
                  <div class="form-top">
                     	<button id="saveBtn" class="btn btn-orange" onclick="savePermission(this)" ><i class="fa fa-save mr5"></i>保存</button>
            	 		<button class="btn btn-default" onclick="closeLayerOpen()" ><i class="fa fa-reply mr5"></i>返回</button>
      			   </div>
              </div>
          <!--main end--> 
      </div>      
</body>
<script type="text/javascript">
$(function(){
	loadMenus();
	$(".js-example-basic-single").select2();
});
function loadMenus(){
	var appId = $("#_appId").val();
	if(appId == null || appId == ''){
		$("#saveBtn").attr('disabled','disabled');
		return;
	}
	$("#saveBtn").remove('disabled');
	var roleId = $("#_roleId").val();
	if(appId == null || appId == '') return;
	$.ajax({
		url:"${ctx}/role/query/permission/detail",
		data:{appId:appId,roleId:roleId},
		dataType:"html",
		type:"post",
		success:function(data){
			$("#menuDetail").html(data);
		}
	});
}

function closeLayerOpen(){
	parent.layer.close(parent.layer.getFrameIndex(window.name));
}

/**
 * 保存权限
 */
function savePermission(obj){
	var appId = $("#_appId").val();
	if(appId == null || appId == ''){
		$(obj).attr('disabled','disabled');
		return;
	}
	
	var roleId = $("#_roleId").val();
	var p = '';
	$("input[name='permissionId']:checked").each(function(i){
		
		if(i==0){
			p = p+$(this).val();
		}else{
			p =p+","+$(this).val()
		}
		
	});
//防止多次点击
	$(obj).attr('disabled','disabled');
	
	$.ajax({
		url:'${ctx}/role/save/permission',
		type:"post",
		data:{roleId:roleId,appId:appId,permissionIds:p},
		dataType:"json",
		success:function(result){
			if(result.code=='success'){
				successTip();
				
			//	 setInterval("parent.layer.close(parent.layer.getFrameIndex(window.name))",2000);
			}else{
				errorTip();
			}
			$(obj).removeAttr('disabled');
		}
	})
}
//点击菜单时，对应的所有功能都选上或不选上
function pMenuClick(obj){
	if($(obj).is(':checked')){
		$(obj).parent('td').next().children('input[type="checkbox"]').each(function(index,o){
			$(o).attr('checked','checked');
		});
	}/* else{
		$(obj).parent('td').next().children('input[type="checkbox"]').each(function(index,o){
			$(o).attr('checked',false);
		});
	} */
}
//点击选择了功能权限选项时，如果对应的菜单没勾上，则把它勾上
function operationClick(obj){
	if($(obj).is(':checked')){
		$(obj).parent('td').prev().children('input[type="checkbox"]').each(function(index,o){
			$(o).attr('checked','checked');
		});
	}else{
		//去掉了勾，建检查，如果一个功能都没勾上了，那么菜单也去掉勾
		var hasOne = false;
		$(obj).parent('td').children('input[type="checkbox"]').each(function(index,o){
			if($(o).is(':checked')){
				hasOne = true;
			}
		});
		if(!hasOne){
			$(obj).parent('td').prev().children('input[type="checkbox"]').each(function(index,o){
				$(o).attr('checked',false);
			});
		}
	}
}
</script>
</html>